$(document).ready(function (){
require(["inputdrop",'tree'] ,function(inputdrop,tree){


var index_list_url = "knowledge/devicedictionary/queryDeviceDictionaryList";

var index_list_col_header = [
							  {name:"",text:"" ,hideSearch:"hide",width:3},
							  {text : "关键字" ,name : "keyWords",align:"left",width:97}
							];

var index_list_col_oper = [
				  			  {icon : "rh-icon rh-edit" ,text : "修改" ,aclick :edit_template_init},
				  			  {icon : "rh-icon rh-delete" ,text : "删除" ,aclick :index_list_delete}
			   			  ];

var list_add_url = "knowledge/devicedictionary/addDeviceDictionary";

var list_update_url = "knowledge/devicedictionary/updDeviceDictionary";

var list_delete_url = "knowledge/devicedictionary/delDeviceDictionary";

var el_table_div = $("#table_div1");

var current_selected_node_id = "root";

var current_selected_node_name = "设备类别";

equip_class_tree();

index_list_get({paramObj:null,isLoad:false,maskObj:"body"});

event_init();

function event_init ()
{
	$("#add_btn").click(function (){
		if(current_selected_node_id == "root")
		{
			g_dialog.operateAlert(null ,"请选择设备类别" ,"error");
			return false;
		}
		edit_template_init();
	});

	$("#delete_btn").click(function (){
		index_list_batch_delete();
	});
}

function equip_class_tree(opt)
{
	if(opt)
	{
		current_selected_node_id = opt;
	}
	um_ajax_post({
		url : "rpc/getCodeList",
		paramObj : {key : "deviceAssetTypeSiemTree"},

		successCallBack : function (data){
			data = data.deviceAssetTypeSiemTree;
			data.push({id:"root" ,parent:"-1" ,label:"设备类别"});
			tree.render($("#equip_class_tree") ,{
				pId:"parentID",
				label:"label",
				id:"id",
				zNodes : data,
				expandNode : "root",
				selectNode : current_selected_node_id,
				zTreeOnClick : function (event, treeId, treeNode){
					current_selected_node_id = treeNode.id;
					current_selected_node_name = treeNode.label;
					var paramObj = {typeId:treeNode.id};
					if(treeNode.id == "root")
					{
						paramObj = null;
					}
					if(treeNode.parentID == "root")
					{
						g_dialog.operateAlert($("[id=equip_class_tree]") ,"请选择"+treeNode.label+"以下的设备类型" ,"error");
						return false;
					}
					else
					{
						index_list_get({
							paramObj:paramObj,
							isLoad:true,
							maskObj:"body"
						});
					}	
				},
			});
		}
	});
}

function index_list_get(option)
{
	g_grid.render($("#table_div1") ,{
 		header : index_list_col_header,
 		oper : index_list_col_oper,
 		operWidth : "100px",
 		url : index_list_url,
 		paramObj : option.paramObj,
 		queryBefore:function(paramObj){
 			paramObj.typeId = current_selected_node_id;

 			if(current_selected_node_id == "root")
 			{
 				paramObj.typeId = "";
 			}						 					
 		},
 		isLoad : option.isLoad,
 		maskObj : option.maskObj,
 		hasBorder : false,
	    autoHeight : true,
	    showCount : true
	});
}

function index_list_batch_delete()
{
	var target_attributed_id = [];
	var rowData = g_grid.getData($("#table_div1") ,{
		chk : true
	});

	for(var i=0 ,len = rowData.length ;i<len ;i++){
		target_attributed_id.push(rowData[i].keyId);
	}

	if(target_attributed_id.length === 0){
		g_dialog.operateAlert($("#table_div1") ,index_select_one_at_least_msg ,"error");
		return false;
	}

	target_attributed_id = target_attributed_id.join(",");

	g_dialog.operateConfirm("确认删除选中的记录么" ,{
		saveclick : function()
		{
			um_ajax_post({
				url : list_delete_url,
				paramObj : {keyId : target_attributed_id},
				successCallBack : function(data){
					g_dialog.operateAlert(null ,"操作成功！");
					var paramObj = {typeId:current_selected_node_id};
					if(current_selected_node_id == "root")
					{
						paramObj = null;
					}
					index_list_get({
						paramObj:paramObj,
						isLoad:true,
						maskObj:"body"
					});
			 		equip_class_tree(current_selected_node_id);
				}
			});
		}
	});
}

function edit_template_init (rowData)
{

	var title = rowData ? "日志特征库修改" : "日志特征库添加";
	$.ajax({
		type: "GET",
		url: "module/base_resource_mant/sec_event_manage/log_characteristic_tpl.html",
		success : function(data){
			g_dialog.dialog($(data).find("[id=edit_template]") ,{
				width : "500px",
				init : init,
				title : title,
				saveclick : save_click
			});
		}
	});

	function init (el)
	{
		g_validate.init(el);

		if (rowData) {
			el.umDataBind("render",rowData);
		}
	}

	function save_click (el ,saveObj)
	{
		if (!g_validate.validate(el))
		{
			return false;
		}

		else
		{
			var flag_url = list_add_url;
			saveObj.typeId = current_selected_node_id;
			if(rowData)
			{
				flag_url = list_update_url;
				saveObj.typeId = current_selected_node_id;
				saveObj.keyId = rowData.keyId;
			}

			um_ajax_get({
				url : flag_url,
				paramObj : saveObj,
				maskObj:el,
				successCallBack : function (data){
					g_dialog.hide(el);
					g_dialog.operateAlert();
					var paramObj = {typeId:current_selected_node_id};
					index_list_get({
						paramObj:paramObj,
						isLoad:false,
					});
					equip_class_tree(current_selected_node_id);
				}
			});
		}
	}
}

function index_list_delete (rowData)
{
	g_dialog.operateConfirm("确认删除此记录么" ,{
		saveclick : function (){
			um_ajax_post({
				url : list_delete_url,
				paramObj : {keyId: rowData.keyId},
				successCallBack : function(data){
					g_dialog.operateAlert(null ,"操作成功！");
					var paramObj = {typeId:current_selected_node_id};
					if(current_selected_node_id == "root")
					{
						paramObj = null;
					}
					index_list_get({
						paramObj:paramObj,
						isLoad:true,
						maskObj:"body"
					});
					equip_class_tree(current_selected_node_id);
				}
			});
		}
	});
}


});
});